/*
 * (C) Copyright 2012, by Tim Xie.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.superapk.diamond.widget;

import com.superapk.diamond.util.DipUtil;
import com.superapk.diamond.util.LogUtil;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.TabWidget;
import android.widget.TextView;

/**
 * 用于消息提示的组件
 * 
 * @author     Tim Xie [tim.xie2008@gmail.com]
 * @date 2013-1-23
 * 
 */
public class NewsView extends TextView {
	
	private final static String LOGTAG = LogUtil.makeLogTag(NewsView.class);
	/**
	 * 左上对齐
	 */
	public static final int POSITION_TOP_LEFT = 1;
	/**
	 * 右上对齐
	 */
	public static final int POSITION_TOP_RIGHT = 2;
	/**
	 * 左下对齐
	 */
	public static final int POSITION_BOTTOM_LEFT = 3;
	/**
	 * 右下对齐
	 */
	public static final int POSITION_BOTTOM_RIGHT = 4;
	/**
	 * 居中显示
	 */
	public static final int POSITION_CENTER = 5;
	
	private static final int DEFAULT_LR_PADDING_DIP = 5;
	/**
	 * 圆角的大小
	 */
	private static final int DEFAULT_CORNER_RADIUS_DIP = 8;
	/**
	 * 默认组件背景色,橘色
	 */
	private static final int DEFAULT_BG_COLOR = Color.rgb(0xFF, 0x7F, 0x00);
	/**
	 * 默认文字颜色
	 */
	private static final int DEFAULT_TEXT_COLOR = Color.WHITE;
	/**
	 * 对齐方式
	 */
	private int mNewsPosition = POSITION_TOP_RIGHT;
	/**
	 * 颜色
	 */
	private int mNewsColor;
	/**
	 * 是否显示
	 */
	private boolean isShown = false;
	/**
	 * 是否显示动画
	 */
	private boolean isAnimate = false;
	/**
	 * 进场动画
	 */
	private Animation mFadeInAnimation;
	/**
	 * 出场动画
	 */
	private Animation mFadeOutAnimation;
	/**
	 * 底层组件
	 */
	private View mRootView;
	/**
	 * 如果底层是TabWidget组件，用于标记当前Tab的Index
	 */
	private int mRootTabWidgetIndex;
	/**
	 * 圆角矩形背景
	 */
	private ShapeDrawable mNewsBg;
	
	private Context mContext;
	
	public NewsView(Context context) {
		this(context, (AttributeSet) null, android.R.attr.textViewStyle);
	}
	
	public NewsView(Context context, AttributeSet attrs) {
		 this(context, attrs, android.R.attr.textViewStyle);
	}
	
	public NewsView(Context context, View target) {
		 this(context, null, android.R.attr.textViewStyle, target, 0);
	}
	
	public NewsView(Context context, TabWidget target, int index) {
		this(context, null, android.R.attr.textViewStyle, target, index);
	}
	
	public NewsView(Context context, AttributeSet attrs, int defStyle) {
		this(context, attrs, defStyle, null, 0);
	}
	
	public NewsView(Context context, AttributeSet attrs, int defStyle, View target, int tabIndex) {
		super(context, attrs, defStyle);
		init(context, target, tabIndex);
	}

	/**
	 * 初始化
	 * @param context
	 * @param target 需要承载的组件
	 * @param tabIndex TabWidget的索引
	 */
	private void init(Context context, View target, int tabIndex) {
		
		this.mContext = context;
		this.mRootView = target;
		this.mRootTabWidgetIndex = tabIndex;
		
		mNewsColor = DEFAULT_BG_COLOR;
		
		setTypeface(Typeface.DEFAULT_BOLD);
		int paddingPixels = DipUtil.dipToPixels(getContext(), DEFAULT_LR_PADDING_DIP);
		setPadding(paddingPixels, 0, paddingPixels, 0);
		setTextColor(DEFAULT_TEXT_COLOR);
		
		//进场动画
		mFadeInAnimation = new AlphaAnimation(0, 1);
		mFadeInAnimation.setInterpolator(new DecelerateInterpolator());
		mFadeInAnimation.setDuration(200);

		//离场动画
		mFadeOutAnimation = new AlphaAnimation(1, 0);
		mFadeOutAnimation.setInterpolator(new AccelerateInterpolator());
		mFadeOutAnimation.setDuration(200);
		
		if (this.mRootView != null) {
			addNewsView(this.mRootView);
		} else {
			show();
		}
		
	}

	/**
	 * 添加NewsView到下层组件
	 * @param rootView
	 */
	private void addNewsView(View rootView) {
		
		LayoutParams lp = rootView.getLayoutParams();
		ViewParent parent = rootView.getParent();
		FrameLayout container = new FrameLayout(mContext);
		
		//TabWidget需要特殊处理
		if (rootView instanceof TabWidget) {
			//找到需要显示的Tab，添加News组件到下层组件
			rootView = ((TabWidget) rootView).getChildTabViewAt(mRootTabWidgetIndex);
			this.mRootView = rootView;
			
			((ViewGroup) rootView).addView(container, 
					new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			
			this.setVisibility(View.INVISIBLE);
			container.addView(this);
			
		} else {
			//添加News组件到下层组件
			ViewGroup group = (ViewGroup) parent; 
			int index = group.indexOfChild(rootView);
			
			group.removeView(rootView);
			group.addView(container, index, lp);
			
			container.addView(rootView);
	
			this.setVisibility(View.INVISIBLE);
			container.addView(this);
			
			group.invalidate();
			
		}
		
	}
	
	/**
	 * 应用对齐方式
	 */
	private void applyLayoutParams() {
		
		FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lp.setMargins(2, 5, 5, 3);
		
		switch (mNewsPosition) {
		case POSITION_TOP_LEFT:
			lp.gravity = Gravity.LEFT | Gravity.TOP;
			break;
		case POSITION_TOP_RIGHT:
			lp.gravity = Gravity.RIGHT | Gravity.TOP;
			break;
		case POSITION_BOTTOM_LEFT:
			lp.gravity = Gravity.LEFT | Gravity.BOTTOM;
			break;
		case POSITION_BOTTOM_RIGHT:
			lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;
			break;
		case POSITION_CENTER:
			lp.gravity = Gravity.CENTER;
			break;
		default:
			break;
		}
		
		setLayoutParams(lp);
		
	}
	
	/**
     * 设置News的对齐方式
     * 
     * @param layoutPosition 参数为 POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT, POSTION_CENTER.
     * 
     */
	public void setBadgePosition(int layoutPosition) {
		if(layoutPosition > POSITION_CENTER || layoutPosition < POSITION_TOP_LEFT){
			throw new IllegalArgumentException("illegal param");
		}
		this.mNewsPosition = layoutPosition;
	}

	/**
     * 设置颜色
     * 
     * @param bgColor
     */
	public void setNewsColor(int newsColor) {
		this.mNewsColor = newsColor;
		mNewsBg = getDefaultBackground();
	}
	
	/**
	 * 设置进场动画
	 * @param fadeInAnimation
	 */
	public void setFadeInAnimation(Animation fadeInAnimation){
		this.mFadeInAnimation = fadeInAnimation;
	}
	
	/**
	 * 设置离场动画
	 * @param fadeOutAnimation
	 */
	public void setFadeOutAnimation(Animation fadeOutAnimation){
		this.mFadeOutAnimation = fadeOutAnimation;
	}
	
	/**
	 * 设置是否显示动画效果
	 * @param anima
	 */
	public void setAnimation(boolean anima){
		this.isAnimate = anima;
	}

	/**
     * 当前是否可见
     * 
     */
	@Override
	public boolean isShown() {
		return isShown;
	}
	
	/**
     * 设置News可见
     * 
     */
	public void show() {
		if (getBackground() == null) {
			if (mNewsBg == null) {
				mNewsBg = getDefaultBackground();
			}
			setBackgroundDrawable(mNewsBg);
		}
		applyLayoutParams();
		
		if (isAnimate && mFadeInAnimation != null) {
			this.startAnimation(mFadeInAnimation);
		}
		this.setVisibility(View.VISIBLE);
		isShown = true;
	}
	
	/**
     * 隐藏News
     * 
     */
	public void hide() {
		if (isAnimate && mFadeOutAnimation != null) {
			this.startAnimation(mFadeOutAnimation);
		}
		this.setVisibility(View.INVISIBLE);
		isShown = false;
	}

	/**
	 * 获取圆角矩形的背景
	 * @return
	 */
	private ShapeDrawable getDefaultBackground() {
		
		int r = DipUtil.dipToPixels(getContext(),DEFAULT_CORNER_RADIUS_DIP);
		float[] outerR = new float[] {r, r, r, r, r, r, r, r};
        
		RoundRectShape rr = new RoundRectShape(outerR, null, null);
		ShapeDrawable drawable = new ShapeDrawable(rr);
		drawable.getPaint().setColor(mNewsColor);
		
		return drawable;
		
	}

}
